package njust.user;
import njust.utils.DBUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDAO {
    public UserDAO(){

    }
    public int searchUser(Login login){
        try {
            List<Map<String, Object>> users = DBUtil.query("user", null);
            for (Map<String, Object> user:users){
                if (user.get("uName").equals(login.getName())){
                    if (!user.get("uPw").equals(login.getPwd())){
                        return 2;
                    }
                    else {
                        if (!user.get("uSchool").equals(login.getCollege())){
                            return 5;
                        }
                        else if (!user.get("uDepartment").equals(login.getSubject())){
                            return 6;
                        }
                        else
                            return 1;
                    }
                }
            }
            return 3;   //未查到账户
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;   //加载错误
    }
    public Login searchLoginByUserId(int uId){
        try {
            List<Map<String, Object>> users = DBUtil.query("user", null);
            for (Map<String, Object> user:users){
                if ((int) user.get("uId")==uId){
                    return new Login((String) user.get("uName"),(String) user.get("uPw"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public int getUserId(Login login){
        Map<String, Object> map=new HashMap<>();
        map.put("uName",login.getName());
        try {
            List<Map<String, Object>> users = DBUtil.query("user", map);
            for (Map<String, Object> user:users){
                if (user.get("uName").equals(login.getName())){
                    return (int) user.get("uId");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
